﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using MvcContrib.UI.Html;
using Peak.Core;
using Peak.Resources;
using System.Threading;

namespace Peak.Web.Controllers.HtmlExtentions
{
  public static class ButtonsExtension
  {
    public static MvcHtmlString CancelButton(this HtmlHelper html)
    {
      return html.PeakButton<BaseController>(f=>f.Back(), Buttons.Cancel);
    }

    public static MvcHtmlString OkButton(this HtmlHelper html)
    {
      return html.PeakSubmitButton(Resources.Buttons.Ok);
    }

    public static MvcHtmlString PeakSubmitButton(this HtmlHelper html,string text)
    {
      return html.Button("button", text, HtmlButtonType.Submit, null, new {@class = "formButton"});
    }

    public static MvcHtmlString PeakSubmitButton<T>(this HtmlHelper html, Expression<Action<T>> action, string text, string formName) where T : Controller
    {
      return html.Button("button", text, HtmlButtonType.Submit,
        string.Format(@"$('#{0}').attr('action','{1}');
          $('#{0}').submit()", formName, html.BuildUrlFromExpression(action)),
        new { @class = "formButton" });
    }

    public static MvcHtmlString SubmitLink<T>(this HtmlHelper html, Expression<Action<T>> action, string text, string formName) where T : Controller
    {
      TagBuilder a = new TagBuilder("a");
      a.Attributes.Add("href", "#");
      a.Attributes.Add("onclick",
                       string.Format(@"$('#{0}').attr('action','{1}');$('#{0}').submit()", formName,
                                     html.BuildUrlFromExpression(action)));
      a.InnerHtml = text;
      return MvcHtmlString.Create(a.ToString());
    }

    public static MvcHtmlString PeakButton<T>(this HtmlHelper html, Expression<Action<T>> action, string text) where T : Controller
    {
      string url = html.BuildUrlFromExpression(action);
      return html.Button("button", text, HtmlButtonType.Button, "document.location='" + url + "'", new { @class = "formButton" });
    }

    public static MvcHtmlString PeakButton(this HtmlHelper html, string onClickCommand, string text)
    {
      return html.Button("button", text, HtmlButtonType.Button, onClickCommand, new { @class = "formButton" });
    }
    
    public static MvcHtmlString PeakRemoveButton<T>(this HtmlHelper html, Expression<Action<T>> action, string formName) where T : Controller
    {
      string url = html.BuildUrlFromExpression(action);
      return html.Button("button", 
        Buttons.Remove, 
        HtmlButtonType.Submit,
        string.Format("return confirmDeleteForm('{0}','{1}','{2}');",Messages.ConfirmDeleteMany,url,formName),        
        new { @class = "formButton" });
    }

    public static MvcHtmlString PeakButton<T>(this HtmlHelper html, Expression<Action<T>> action, string @params, string text) where T : Controller
    {
      string url = html.BuildUrlFromExpression(action);
      return html.Button("button", text, HtmlButtonType.Button, "document.location='" + url + "?'+"+@params, new { @class = "formButton" });
    }

    public static MvcHtmlString Delete<T>(this HtmlHelper html, Expression<Action<T>> action) where T : Controller
    {
      string url = html.BuildUrlFromExpression(action);
      TagBuilder img = new TagBuilder("img");
      img.Attributes.Add("src",html.ResolveUrl("~/Content/Images/trash.png"));
      img.Attributes.Add("onclick","return confirmDelete('"+Messages.ConfirmDelete+"','"+url+"')");
      TagBuilder div = new TagBuilder("div");
      div.Attributes.Add("title", ToolTips.Delete);
      div.AddCssClass("action");
      div.InnerHtml = img.ToString();
      return MvcHtmlString.Create(div.ToString());
    }

    public static MvcHtmlString DeleteMany<T>(this HtmlHelper html, Expression<Action<T>> action, string formName) where T : Controller
    {
      TagBuilder img = new TagBuilder("img");
      img.Attributes.Add("src", html.ResolveUrl("~/Content/Images/trash.png"));
      img.Attributes.Add("onclick",
                         string.Format("return confirmDeleteForm('{0}','{1}','{2}');", 
                                       Messages.ConfirmDeleteMany,
                                       html.BuildUrlFromExpression(action), formName));         
      TagBuilder div = new TagBuilder("div");
      div.Attributes.Add("title", ToolTips.Delete);
      div.AddCssClass("action");
      div.InnerHtml = img.ToString();
      return MvcHtmlString.Create(div.ToString());
    }

    public static MvcHtmlString Edit<T>(this HtmlHelper html, Expression<Action<T>> action) where T : Controller
    {
      string url = html.BuildUrlFromExpression(action);
      TagBuilder img = new TagBuilder("img");
      img.Attributes.Add("src",html.ResolveUrl("~/Content/Images/edit.png"));      
      TagBuilder a = new TagBuilder("a");
      a.AddCssClass("action");
      a.Attributes.Add("href",url);
      a.Attributes.Add("title", ToolTips.Edit);
      a.InnerHtml = img.ToString();
      return MvcHtmlString.Create(a.ToString());      
    }

    public static MvcHtmlString EditMany<T>(this HtmlHelper html, Expression<Action<T>> action, string formName) where T : Controller
    {
      TagBuilder img = new TagBuilder("img");
      img.Attributes.Add("src", html.ResolveUrl("~/Content/Images/edit.png"));
      TagBuilder a = new TagBuilder("a");
      a.AddCssClass("action");
      a.Attributes.Add("href", "#");
      a.Attributes.Add("onclick",
                       string.Format(@"$('#{0}').attr('action','{1}');$('#{0}').submit()",
                                     formName,
                                     html.BuildUrlFromExpression(action)));
      a.Attributes.Add("title", ToolTips.Edit);
      a.InnerHtml = img.ToString();
      return MvcHtmlString.Create(a.ToString());
    }

    public static MvcHtmlString AssignToMe<T>(this HtmlHelper html, Expression<Action<T>> action,User assigned) where T : Controller
    {
      if(assigned != null && assigned.UserName == Thread.CurrentPrincipal.Identity.Name)
        return null;

      string url = html.BuildUrlFromExpression(action);
      TagBuilder img = new TagBuilder("img");
      img.Attributes.Add("src", html.ResolveUrl("~/Content/Images/assignToMe.png"));
      TagBuilder a = new TagBuilder("a");
      a.AddCssClass("action");
      a.Attributes.Add("href", url);
      a.Attributes.Add("title", ToolTips.AssignToMe);
      a.InnerHtml = img.ToString();
      return MvcHtmlString.Create(a.ToString());
    }

    public static MvcHtmlString AssignToMeMany<T>(this HtmlHelper html, Expression<Action<T>> action, string formName) where T : Controller
    {
      string url = html.BuildUrlFromExpression(action);
      TagBuilder img = new TagBuilder("img");
      img.Attributes.Add("src", html.ResolveUrl("~/Content/Images/assignToMe.png"));
      TagBuilder a = new TagBuilder("a");
      a.AddCssClass("action");
      a.Attributes.Add("href", "#");
      a.Attributes.Add("onclick",
                       string.Format(@"$('#{0}').attr('action','{1}');$('#{0}').submit()", 
                                     formName,
                                     html.BuildUrlFromExpression(action)));
      a.Attributes.Add("title", ToolTips.AssignToMe);
      a.InnerHtml = img.ToString();
      return MvcHtmlString.Create(a.ToString());
    }

    public static MvcHtmlString GoToStory<T>(this HtmlHelper html, Expression<Action<T>> action) where T : Controller
    {
      string url = html.BuildUrlFromExpression(action);
      TagBuilder img = new TagBuilder("img");
      img.Attributes.Add("src", html.ResolveUrl("~/Content/Images/story.png"));
      TagBuilder a = new TagBuilder("a");
      a.AddCssClass("action");
      a.Attributes.Add("href", url);
      a.Attributes.Add("title", ToolTips.GoToStory);
      a.InnerHtml = img.ToString();
      return MvcHtmlString.Create(a.ToString());
    }


    public static MvcHtmlString Print<T>(this HtmlHelper html, Expression<Action<T>> action) where T : Controller
    {
      string url = html.BuildUrlFromExpression(action);
      TagBuilder img = new TagBuilder("img");
      img.Attributes.Add("src", html.ResolveUrl("~/Content/Images/print.png"));
      TagBuilder a = new TagBuilder("a");
      a.AddCssClass("action");
      a.Attributes.Add("href", url);
      a.Attributes.Add("title", ToolTips.Print);
      a.InnerHtml = img.ToString();
      return MvcHtmlString.Create(a.ToString());
    }

  }
}
